/**
 * 
 */
package se.ricardo.gamesorgs.client;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JToolBar;


/**
 * Main frame of the application for displaying specific views.
 * <br>
 * 
 * @author Ricardo Ocaña Leal
 *
 */
public class GamesOrganizerGui extends JFrame {
	private JToolBar toolBar;
	
	// JToolBar components.
	public static final int ADD_BUTTON = 0;
	public static final int EDIT_BUTTON = 1;
	public static final int DELETE_BUTTON = 2;
	public static final int SEARCH_BUTTON = 3;
	
	/**
	 * Initializes the frame with certain default values.
	 * */
	public GamesOrganizerGui() {
		setSize(1024, 768);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setTitle("Games Organizer");
		setLayout(new BorderLayout());
		
		// Get the screen dimensions and place the app screen
		// at the center of it.
		Dimension screenDimension = Toolkit.getDefaultToolkit().getScreenSize();
		setLocation(screenDimension.width/2 - 1024/2,
				screenDimension.height/2 - 768/2);
		
		setJMenuBar(createMenuBar());
		createToolBar();
		
		setVisible(true);
	}
	
	/**
	 * Sets the contents of the frame.
	 * <br>
	 * 
	 * @param
	 * modePanel The view chosen by the user to be displayed in
	 * the frame.
	 * */
	public void setContentMode(JPanel modePanel) {
		Container pane = getContentPane();
		pane.removeAll();
		pane.add(toolBar, BorderLayout.NORTH);
		pane.add(modePanel, BorderLayout.CENTER);
	}
	
	/**
	 * Creates a {@link JMenuBar} for the frame.
	 * <br>
	 * 
	 * @return
	 * The JMenuBar.
	 * */
	private JMenuBar createMenuBar() {
		final JMenuBar menuBar = new JMenuBar();
		
		final JMenu fileMenu = new JMenu("File");
		menuBar.add(fileMenu);
		
		JMenuItem menuItem = new JMenuItem("Create Backup");
		fileMenu.add(menuItem);
		menuItem = new JMenuItem("Load Backup");
		fileMenu.add(menuItem);
		
		fileMenu.addSeparator();
		
		menuItem = new JMenuItem("Exit");
		fileMenu.add(menuItem);
		
		final JMenu modeMenu = new JMenu("Mode");
		menuBar.add(modeMenu);
		
		ButtonGroup group = new ButtonGroup();
		JRadioButtonMenuItem gamesMode = new JRadioButtonMenuItem("Games Mode");
		gamesMode.setSelected(true);
		group.add(gamesMode);
		modeMenu.add(gamesMode);
		
		JRadioButtonMenuItem systemsMode = new JRadioButtonMenuItem("Systems Mode");
		group.add(systemsMode);
		modeMenu.add(systemsMode);
		
		modeMenu.addSeparator();
		
		group = new ButtonGroup();
		JRadioButtonMenuItem personalMode = new JRadioButtonMenuItem("Personal Mode");
		personalMode.setSelected(true);
		group.add(personalMode);
		modeMenu.add(personalMode);
		
		JRadioButtonMenuItem storageMode = new JRadioButtonMenuItem("Storage Mode");
		group.add(storageMode);
		modeMenu.add(storageMode);
		
		final JMenu helpMenu = new JMenu("Help");
		menuBar.add(helpMenu);
		menuItem = new JMenuItem("Help Contents");
		helpMenu.add(menuItem);
		helpMenu.addSeparator();
		menuItem = new JMenuItem("About Games Organizer");
		helpMenu.add(menuItem);
		
		return menuBar;
	}
	
	/**
	 * Creates the {@link JToolBar} and adds the desired
	 * components.
	 * <br>
	 * */
	private void createToolBar() {
		toolBar = new JToolBar();
		
		JButton button = new JButton("Add");
		toolBar.add(button);
		button = new JButton("Edit");
		toolBar.add(button);
		button = new JButton("Delete");
		toolBar.add(button);
		button = new JButton("Search");
		toolBar.add(button);
	}
	
	/**
	 * Called to get the {@link JToolBar} of the frame.
	 * <br>
	 * 
	 * @return
	 * Object of the {@link JToolBar}.
	 * */
	public JToolBar getToolBar() {
		return toolBar;
	}
	
	/**
	 * Adds a windowListener to the frame, for catching events
	 * triggered by the user.
	 * <br>
	 * 
	 * @param
	 * w The {@link WindowAdapter} to be set to the frame.
	 * */
	public void registerWindowListener(WindowAdapter w) {
		addWindowListener(w);
	}
}
